import { defineStore } from "pinia";
import axios from "axios";

const useCinemaStore = defineStore("cinemas", {
  state: () => ({
    cinemaList: [],
  }),

  actions: {
    async getCinemaList() {
      const res = await axios({
        url: "https://m.maizuo.com/gateway?cityId=110100&ticketFlag=1&k=7546382",
        headers: {
          "X-Client-Info":
            '{"a":"3000","ch":"1002","v":"5.2.1","e":"16927511235634589070458881","bc":"110100"}',
          "X-Host": "mall.film-ticket.cinema.list",
        },
      });
      //这里如果改为箭头函数，this指向会有问题，不是当前的store了，变成window了
      this.cinemaList = res.data.data.cinemas;
    },
  },
  getters: {
    computedCinemaList(state) {
      //如果需要传参，需要写成返回一个函数形式
      return (type) => {
        return state.cinemaList.filter((item) => item.eTicketFlag === type);
      };
    },
  },
});

export default useCinemaStore;
